/***********************************************
 @Name :        小铭同学
 @Time :        2025-08-15
 @Email :       LiaoMingWJ233@163.com
 @Comment :     功能窗口 智能对话
 @FileName :    widget_funca_smart_chat.cpp
 @Version :     1.0
************************************************/

#include "widget_funca_smart_chat.h"
#include "ui_widget_funca_smart_chat.h"

Widget_funcA_Smart_chat::Widget_funcA_Smart_chat(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget_funcA_Smart_chat)
{
    ui->setupUi(this);
    
    deepseek = new Deepseek_Chat(this);               
    connect(deepseek , &Deepseek_Chat::replyReady , this, [&](const QString &reply){
        qDebug() << reply << "接收到信息";
        addMessage(":/头像", "小谭", reply+" ", false);

    });
    connect(deepseek , &Deepseek_Chat::error_signals , this,[&](const QString &reply){
        qDebug() << reply << "接收到错误";
    });

    // 设置带背景色和圆角的QScrollArea
    ui->scrollArea->setStyleSheet(
        "QScrollArea {"
        "   background-color: rgba(204, 235, 255, 255);"  // 白色背景，180透明度
        "   border-radius: 13px;"                         // 10像素圆角
        "   border: 1px solid #ffffff;"                   // 白色边框
        "}"
        
        "QScrollArea > QWidget > QWidget {"
        "   background-color: transparent;"               // 内部内容部件透明
        "}"
        "QScrollBar:vertical {"
        "   border: none;"
        "   background: rgba(240, 240, 240, 100);"       // 滚动条背景
        "   width: 10px;"                                // 滚动条宽度
        "   margin: 0px;"
        "}"
        "QScrollBar::handle:vertical {"
        "   background: rgba(200, 200, 200, 150);"       // 滚动条手柄
        "   min-height: 20px;"                           // 最小高度
        "   border-radius: 5px;"                         // 圆角
        "}"
    );
    
    // 初始化 scrollArea
    scrollArea = ui->scrollArea;
    scrollArea->setFixedSize(696, 406); // 固定大小
    scrollArea->setWidgetResizable(true);
    
    // 设置容器 widget
    QWidget *scrollContent = new QWidget();
    scrollLayout = new QVBoxLayout(scrollContent);
    scrollLayout->setAlignment(Qt::AlignTop);
    scrollLayout->setSpacing(10);
    
    scrollArea->setWidget(scrollContent);                  
    
    // 设定按钮大小
    ui->Button_send->setFixedSize(80 , 30);
    QFont font("楷体", 35);
    font.setBold(true);  // 启用加粗
    ui->Button_send->setFont(font);
    

    // 设置背景透明
    ui->textEdit->setStyleSheet("background: transparent;");
    // 设置透明背景并去除边框
    ui->textEdit->setStyleSheet("background: transparent; border: none;");
    // 确保文本框能够正常输入
    ui->textEdit->setAttribute(Qt::WA_OpaquePaintEvent, false);
    // 设置字体
    ui->textEdit->setFont(QFont("楷体" , 13));
    
    
    // 设定消息对话框为垂直布局
    Message_layout = new QVBoxLayout(ui->widget_show);
    
    // 连接发射信号和槽函数
    connect(ui->Button_send , &Button_Cust_Base::signals_click , this , &Widget_funcA_Smart_chat::Slota_Add_Send_Message);

}

Widget_funcA_Smart_chat::~Widget_funcA_Smart_chat()
{
    delete ui;
}

void Widget_funcA_Smart_chat::addMessage(const QString &avatarPath, const QString &senderName, const QString &message, bool isMe)
{
    ChatBubble *bubble = new ChatBubble();
    bubble->setMessage(avatarPath, senderName, message, isMe);
    
    // 添加到 scrollArea 的布局
    scrollLayout->addWidget(bubble);
    
    // 滚动到底部
    QScrollBar *vScrollBar = scrollArea->verticalScrollBar();
    vScrollBar->setValue(vScrollBar->maximum());
}

void Widget_funcA_Smart_chat::Slota_Add_Send_Message()
{
    if(!ui->textEdit->toPlainText().isEmpty())
    {
        // 创建一个发送对话框
        chat_message = new Widget_smart_chat_message(ui->scrollArea);
        // 添加到对话框窗口   Widget_smart_chat_message
        Message_layout->addWidget(chat_message);      
        
        // 强制更新布局
        ui->scrollArea->update();
        
        // 设定为发送模式
        chat_message->set_Send_Layout();
        
        // 显示边框
        chat_message->show();
        chat_message->move( 100 , 100);
        qDebug() << chat_message->rect();
        qDebug() << this->rect();
        
        
        // 获取文本
        chat_message->set_Edit_Text(ui->textEdit->toPlainText());
        
        // 发送请求
        deepseek->ask(ui->textEdit->toPlainText());
        
        addMessage(":/头像lm", "罗峰", ui->textEdit->toPlainText()+" ", true);
        qDebug()<<ui->textEdit->toPlainText();
        
        // 清空输入框文本
        ui->textEdit->clear();
    }
    
    this->update();
}

